package com.lobo.blog.controller.admin;

import com.github.pagehelper.PageInfo;
import com.lobo.blog.entity.Type;
import com.lobo.blog.exception.AllReadyBeenDeletedException;
import com.lobo.blog.service.AdminTypeService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @authoer:MacieSerenity
 * @createDate: 2022/9/29
 * @description:
 */
@Controller
@RequestMapping("/admin")
public class AdminTypesController {

    @Resource
    AdminTypeService adminTypeService;

    @RequestMapping(value = "/types/{pageNum}", method = RequestMethod.GET)
    public String toAdminTypesPage(
            @PathVariable(value = "pageNum", required = false) Integer pageNum,
            Model model) {
        if (pageNum == null) {
            pageNum = 1;
        }
        PageInfo<Type> typePageInfo = adminTypeService.getTypes(pageNum);
        model.addAttribute("page", typePageInfo);
        return "/admin/types";
    }


    @RequestMapping(value = "/types/input")
    public String addNewType(@RequestParam("typeName") String typeName, Model model) {
        Type type = new Type(0, typeName, null, new Date(), new Date(), 0, null);
        int result = adminTypeService.saveType(type);
        if (result == 0) {
            model.addAttribute("msg", "添加失败！");
        } else {
            model.addAttribute("msg", "添加成功！");
        }
        PageInfo<Type> typePageInfo = adminTypeService.getTypes(1);
        model.addAttribute("page", typePageInfo);
        return "/admin/types";
    }

    @RequestMapping("/types/delete/{id}")
    public String deleteType(@PathVariable("id") Integer id,
                             Model model) {
        try {
            if (adminTypeService.deleteTypeById(id) == 1) {
                model.addAttribute("msg", "删除成功！");
            } else {
                model.addAttribute("msg", "删除失败！该分类已经不存在！！");
            }
        } catch (AllReadyBeenDeletedException e) {
            throw new RuntimeException(e);
        }
        PageInfo<Type> typePageInfo = adminTypeService.getTypes(1);
        model.addAttribute("page", typePageInfo);
        return "/admin/types";
    }

    @RequestMapping(value = "/types/update", method = RequestMethod.POST)
    public String updateType(@RequestParam(value = "typeId") Integer typeId,
                             @RequestParam(value = "typeName") String typeName,
                             Model model) {
        Type type = new Type(typeId, typeName, null, null, null, null, null);
        int result = adminTypeService.updateType(type);
        if (result != 0) {
            model.addAttribute("msg", "更新成功！");
        } else {
            model.addAttribute("msg", "更新失败！已有同名的分类！");
        }
        PageInfo<Type> typePageInfo = adminTypeService.getTypes(1);
        model.addAttribute("page", typePageInfo);
        return "/admin/types";
    }

}
